import time

from selenium import webdriver
from selenium.webdriver.common.by import By

from unittest import TestCase

class TestLogin(TestCase):

    def  testLogin1(self):
        # 1.准备数据
        username = "lisi"
        password = "123456"
        expect = "个人理财系统"
        # 2.执行操作
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        # 定位用户名输入框
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)

        # 定位密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)

        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()

        time.sleep(2)

        # //*[@id="logo"]/a
        # 定位标题，并获取元素数据
        result = driver.find_element(By.XPATH, '//*[@id="logo"]/a').text  # text 获取文本数据


        if result !=  expect:  # 用于截图
            driver.save_screenshot("lisi----123456.jpg")

        self.assertEqual(result,expect)  # 这一步主要在后面界面报告能看的见

        # 关闭浏览器
        driver.quit()
    def testLogin2(self):
    #1.准备数据
         username='admin'
         password='123456'
         expect="后台管理系统"
    #2.执行操作
         driver=webdriver.Chrome()
         driver.get("http://localhost:90")
         driver.maximize_window()
    #3.定位输入框
         driver.find_element(By.XPATH,"//input[@id='username']").send_keys(username)
         driver.find_element(By.XPATH,"//input[@id='password']").send_keys(password)
    #4.点击登录
         driver.find_element(By.XPATH,"//button[@id='login_btn']").click()
         time.sleep(2)
    #5.获取结果
         result=driver.find_element(By.XPATH,"//*[@id='logo']/a").text
         if result!= expect:
             driver.save_screenshot('admin-----123456.png')
         self.assertEqual(result,expect)
    #6.关闭浏览器
         driver.quit()

    def testLogin3(self):
    #1.准备数据
      username='lisi3'
      password='123456'
      expect='用户名不存在'
    #2.执行操作
      driver=webdriver.Chrome()
      driver.get("http://localhost:90")
      driver.maximize_window()
      driver.find_element(By.XPATH,"//input[@id='username']").send_keys(username)
      driver.find_element(By.XPATH,"//input[@id='password']").send_keys(password)
      driver.find_element(By.XPATH,"//button[@id='login_btn']").click()
      time.sleep(2)
    #3.获取结果
      result=driver.find_element(By.XPATH,"/html/body/div/div/div/form/div[1]/span[1]").text
      if expect != result:
          driver.save_screenshot("lisi3----123456.jpg")
      self.assertEqual(result,expect)
    #4.关闭浏览器
      driver.close()

    def testLogin4(self):
        username='lisi'
        password='1234567'
        expect='密码错误'
         #2.执行操作
        driver=webdriver.Chrome()
        driver.get('http://localhost:90')
        driver.maximize_window()

        driver.find_element(By.XPATH,"//input[@id='username']").send_keys(username)
        driver.find_element(By.XPATH,'//input[@id="password"]').send_keys(password)
        driver.find_element(By.XPATH,'//button[@id="login_btn"]').click()
        time.sleep(2)

        result=driver.find_element(By.XPATH,'/html/body/div/div/div/form/div[2]/span[1]').text
        if result!=expect:
            driver.save_screenshot('lisi------1234567')
        self.assertEqual(expect,result)
        driver.close()

    def testLogin5(self):
        username='admin1'
        password='123456'
        expect='用户名不存在'
        driver=webdriver.Chrome()
        driver.get('http://localhost:90')
        driver.maximize_window()
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        driver.find_element(By.XPATH, '//input[@id="password"]').send_keys(password)
        driver.find_element(By.XPATH, '//button[@id="login_btn"]').click()
        time.sleep(2)
        result=driver.find_element(By.XPATH,'/html/body/div/div/div/form/div[1]/span[1]').text
        if result != expect:
            driver.save_screenshot('admin1-----123456')
        driver.close()
    def testLogin6(self):
        username='admin'
        password='123457'
        expect='密码错误'
        driver=webdriver.Chrome()
        driver.get('http://localhost:90')
        driver.maximize_window()
        driver.find_element(By.XPATH,'//input[@id="username"]').send_keys(username)
        driver.find_element(By.XPATH,'//input[@id="password"]').send_keys(password)
        driver.find_element(By.XPATH,'//button[@id="login_btn"]').click()
        time.sleep(2)
        result=driver.find_element(By.XPATH,'/html/body/div/div/div/form/div[2]/span[1]').text
        if result != expect:
            driver.save_screenshot('admin-----1234567')
        self.assertEqual(expect,result)
        driver.close()
